//
//  MSCustomerMatchingVC.m
//  SocietySale
//
//  Created by Mike on 16/11/18.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSCustomerMatchingVC.h"
#import "MSCustomerMatchingTableVC.h"
#import "MSCustomerMatchingCell.h"

@interface MSCustomerMatchingVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) MSCustomerMatchingTableVC *tableVC;
@property (nonatomic, strong) UITableView    *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MSCustomerMatchingVC

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = @"客户追踪";
}

- (void)initUI{
//    self.tableVC = [[MSCustomerMatchingTableVC alloc] initWithStyle:UITableViewStyleGrouped];
//    self.tableVC.view.frame = self.contentView.bounds;
//    self.tableVC.enableRefresh = NO;
//    self.tableVC.enableRequest = NO;
//    self.tableVC.delegate = (id)self;
//    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self.contentView addSubview:self.tableVC.view];
    
    _tableView = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = kDefaultViewBackgroundColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:_tableView];
}

- (void)testDataFunc{
    MSDetailContactModel *model = [[MSDetailContactModel alloc] init];
    model.name = @"班辉";
    model.position = @"项目经理";
    model.company = @"上海电力有限公司";
    model.telephone = @"18754486786";
    model.source = @"上海市宝山区友谊路89号";
    
    MSDetailContactModel *model2 = [[MSDetailContactModel alloc] init];
    model2.name = @"王海峰";
    model2.position = @"项目经理";
    model2.company = @"上海电力有限公司";
    model2.telephone = @"18754486786";
    model2.source = @"上海市宝山区友谊路89号";
    
//    self.tableVC.dataArray = [@[@[model,model,model],@[model2,model2,model2]] mutableCopy];
    self.tableVC.pageNum = 1;
    [self request:NO];
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"CustomerMatching"]!=nil){
//            self.tableVC.totalPagesNum = [responseDic[@"totalPage"] intValue];
//            if (self.tableVC.pageNum == 1) {
//                self.tableVC.dataArray = nil;
//            }
            if (responseDic[@"state"]!=nil){
                NSMutableArray *dataArrray = [[NSMutableArray alloc] initWithArray:self.tableVC.dataArray];
                [dataArrray addObjectsFromArray:responseDic[@"CustomerMatching"]];
                
                [self reloadContacts:dataArrray];
            }
            else{
                self.tableVC.dataArray = self.tableVC.dataArray;
            }
        }
    }
}

- (void)reloadContacts:(NSArray *)contactsTemp{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    if (contactsTemp.count<1) {
        return;
    }
    UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];
    for (MSDetailContactModel *contacter in contactsTemp) {
        NSInteger sect = [theCollation sectionForObject:contacter
                                collationStringSelector:@selector(getContacterName)];
        contacter.section = sect;
    }
    
    NSInteger highSection = [[theCollation sectionTitles] count];
    NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection];
    for (NSInteger i = 0; i <= highSection; i++) {
        NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
        [sectionArrays addObject:sectionArray];
    }
    
    for (SYContacter *addressBook in contactsTemp) {
        [(NSMutableArray *)[sectionArrays objectAtIndex:addressBook.section] addObject:addressBook];
    }
    
    //getContactName 如果这个返回的是nil会有问题
    for (NSMutableArray *sectionArray in sectionArrays) {
        NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:@selector(getContacterName)];
        [_dataArray addObject:sortedSection];
    }
    [self.tableView reloadData];
}

#pragma mark -
#pragma mark UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_dataArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[_dataArray objectAtIndex:section] count] ? [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section] : nil;
}

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    view.tintColor = [UIColor whiteColor];
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    header.contentView.backgroundColor=kDefaultViewBackgroundColor;
    header.textLabel.font = HeitiFont(15);
    header.textLabel.textColor = kDarkGrayColor;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return [[_dataArray objectAtIndex:section] count] ? tableView.sectionHeaderHeight : 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[_dataArray objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellId = @"cellId";
    MSCustomerMatchingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[MSCustomerMatchingCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.row == [[_dataArray objectAtIndex:indexPath.section] count] -1) {
        cell.horizonLineView.frame = CGRectMake(0, 113-0.6, SCREEN_WIDTH, 0.6);
    }
    else{
        cell.horizonLineView.frame = CGRectMake(12, 113-0.6, SCREEN_WIDTH-24, 0.6);
    }
    cell.model = [[_dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 113;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}



#pragma mark DYTableViewProtocol
/** 上拉 */
- (void)pullTableView:(UITableView *)tableView isNextPage:(BOOL)isNextPage{
    self.tableVC.pageNum++;
    [self request:NO];
}

- (void)pullRefreshRequest
{
    self.tableVC.pageNum = 1;
    [self request:YES];
}

-(void)request:(BOOL)isPull
{
    UIView * hudView = isPull?nil:self.contentView;
    NSString *pageNumber = [NSString stringWithFormat:@"%d",self.tableVC.pageNum];
    NSDictionary *dict = @{
                           @"pageNo":pageNumber,
                           @"pageSize":@"100",
                           };
    [netEngine requestCustomerMatchingWithTarget:self.VctrID hudView:hudView params:dict];
}



@end
